/*
 * @Author: 陈德基
 * @Date: 2021-11-15 16:37:53
 * @LastEditTime: 2021-11-28 11:39:49
 * @Description: 
 * @FilePath: \copy_gsy_github_app_flutter\lib\page\main\subpage\trend.dart
 * 联系方式：
 * wx: melody_2009
 * qq: 7815171323 
 */

import 'dart:js';

import 'package:animations/animations.dart';
import 'package:copy_gsy_github_app_flutter/page/main/controllers/controller.dart';
import 'package:copy_gsy_github_app_flutter/widget/empty_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/rendering/sliver_persistent_header.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class TrendWidget extends StatefulWidget {
  TrendWidget({Key? key}) : super(key: key);

  @override
  _TrendWidgetState createState() => _TrendWidgetState();
}

class _TrendWidgetState extends State<TrendWidget>
    with SingleTickerProviderStateMixin {
  RefreshController _refreshController =
      RefreshController(initialRefresh: false);

  List<Widget> _sliverBuilder(BuildContext context, bool innerBoxIsScrolled) {
    return <Widget>[
      SliverPersistentHeader(
          delegate: APPSliverHeaderDelegate(
              minHeight: 65.h,
              maxHeight: 65.h,
              snapConfig: FloatingHeaderSnapConfiguration(
                  curve: Curves.bounceInOut,
                  duration: Duration(milliseconds: 10)),
              vSyncs: this))
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SmartRefresher(
          controller: _refreshController,
          child: NestedScrollView(
              headerSliverBuilder: (context, innerBoxIsScrolled) {
                return _sliverBuilder(context, innerBoxIsScrolled);
              },
              body: _TrendControllerWidget())),
    );
  }
}

class _TrendControllerWidget extends GetView<MainController> {
  _renderTrendItem(trend) {
    return Container(); // TODO 每个趋势的Item布局
  }

  @override
  Widget build(BuildContext context) {
    return controller.state.trends.length > 0
        ? Obx(() => ListView.builder(
              itemBuilder: (context, index) {
                return _renderTrendItem(controller.state.trends[index]);
              },
              physics: AlwaysScrollableScrollPhysics(),
              itemCount: controller.state.trends.length,
            ))
        : Container(
            child: Center(
              child: EmptyView(),
            ),
          );
  }
}

class APPSliverHeaderDelegate extends SliverPersistentHeaderDelegate {
  APPSliverHeaderDelegate(
      {required this.minHeight,
      required this.maxHeight,
      required this.snapConfig,
      required this.vSyncs,
      this.child,
      this.builder,
      this.changeSize = false});

  final double minHeight;
  final double maxHeight;
  final Widget? child;
  final Builder? builder;
  final TickerProvider vSyncs;
  final bool changeSize;
  final FloatingHeaderSnapConfiguration snapConfig;
  AnimationController? animationController;

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return builder == null
        ? child!
        : builder!(context, shrinkOffset, overlapsContent);
  }

  @override
  double get maxExtent => maxHeight;

  @override
  double get minExtent => minHeight;

  @override
  bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) {
    return true;
  }

  @override
  FloatingHeaderSnapConfiguration? get snapConfiguration => snapConfiguration;

  @override
  TickerProvider? get vsync => vSyncs;
}

typedef Widget Builder(
    BuildContext context, double shrinkOffset, bool overlapsContent);
